package view.com;


import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.FocusEvent;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author willy
 */
public class PgButton extends JButton{

    Image fond;
    Image temp_fond_norm;
    Image temp_fond_enf;
    Image temp_fond_focus;
    public PgButton() {
        super();
        ImageIcon imgi=new javax.swing.ImageIcon(getClass().getResource("/img/boutonvide.png"));
        temp_fond_norm=imgi.getImage();
        imgi=new javax.swing.ImageIcon(getClass().getResource("/img/boutonvide.png"));
        temp_fond_enf=imgi.getImage();
        imgi=new javax.swing.ImageIcon(getClass().getResource("/img/boutonvide.png"));
        temp_fond_focus=imgi.getImage();
        fond=temp_fond_norm;
        setBorderPainted(false);
        addAction();

    }

    public PgButton(String text) {
        ImageIcon imgi=new javax.swing.ImageIcon(getClass().getResource("/img/boutonvide.png"));
        temp_fond_norm=imgi.getImage();
        imgi=new javax.swing.ImageIcon(getClass().getResource("/img/boutonvide.png"));
        temp_fond_enf=imgi.getImage();
        imgi=new javax.swing.ImageIcon(getClass().getResource("/img/boutonvide.png"));
        temp_fond_focus=imgi.getImage();
        fond=temp_fond_norm;
        setBorderPainted(false);
        setText(text);
        addAction();
    }

    public PgButton(String text,String img) {
        ImageIcon imgi=new javax.swing.ImageIcon(getClass().getResource("/img/"+img));
        temp_fond_norm=imgi.getImage();
        imgi=new javax.swing.ImageIcon(getClass().getResource("/img/"+img));
        temp_fond_enf=imgi.getImage();
        imgi=new javax.swing.ImageIcon(getClass().getResource("/img/"+img));
        temp_fond_focus=imgi.getImage();
        fond=temp_fond_norm;
        setBorderPainted(false);
        setText(text);
        addAction();
    }

    public void addAction()
    {
        this.addMouseListener(new java.awt.event.MouseListener() {
            public void mouseClicked(MouseEvent e) {
            }

            public void mousePressed(MouseEvent e) {
                //fond=temp_fond_enf;
            }

            public void mouseReleased(MouseEvent e) {
                fond=temp_fond_norm;
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }
        });
        this.addFocusListener(new java.awt.event.FocusListener() {

            public void focusGained(FocusEvent e) {
                fond=temp_fond_focus;
            }

            public void focusLost(FocusEvent e) {
                fond=temp_fond_norm;
            }
        });
    }
    @Override
    protected void paintComponent(Graphics g) {
        //super.paintComponent(g);
        g.drawImage(fond,0,0,getWidth(), getHeight(), this);
        g.drawString(getText(), (getWidth()/2)-(getText().length()*3) , (getHeight()/2)+(3));
    }



    public static void main(String[] arg)
    {
        JFrame jf=new JFrame("exple");
        jf.setSize(400,400);
        jf.setLayout(null);
        PgButton pg1=new PgButton("OK");
        PgButton pg2=new PgButton("Annuler");
        //pg.setText("Salut");
        pg1.setBounds(20,40,100,30);
        pg2.setBounds(200,40,100,30);
        jf.add(pg1);
        jf.add(pg2);
        jf.setVisible(true);
    }

}
